﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using RxjhServer;

namespace RxjhTool
{
	public partial class YbQForm : Form
	{
		private byte[] byte_1A24628 = new byte[256];

		private Dictionary<int, 任务类> 任务 = new Dictionary<int, 任务类>();

		private string 文件地区 = "";

		private string 文件时间 = "";

		private string 增加内容 = "\r\n\r\n灵天江湖一直被模仿，从未被超越！";

		private static int 锁定长度;

		public YbQForm()
		{
			this.InitializeComponent();
			int num = 0;
			do
			{
				this.byte_1A24628[num] = (byte)((num >> 4 & 1) | (num >> 2 & 24) | (num >> 1 & 64) | 2 * ((num & 3) | 4 * ((num & 4) | 2 * (num & 248))));
				num++;
			}
			while (num < 256);
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			this.toolStripStatusLabel1.Text = "任务总数:" + this.任务.Count;
		}

		private int getlen(byte[] var, ref int 处理长度)
		{
			int num = 处理长度++;
			for (int i = num; i < var.Length; i++)
			{
				if (var[i] == 32 || var[i] == 10)
				{
					byte[] array = new byte[i - num];
					Buffer.BlockCopy(var, num, array, 0, array.Length);
					string @string = Encoding.Default.GetString(array);
					处理长度 += array.Length;
					return int.Parse(@string);
				}
			}
			return 0;
		}

		private string getString(byte[] var, ref int 处理长度)
		{
			int num = 处理长度++;
			for (int i = num; i < var.Length; i++)
			{
				if (var[i] == 32 || var[i] == 10)
				{
					byte[] array = new byte[i - num];
					Buffer.BlockCopy(var, num, array, 0, array.Length);
					string @string = Encoding.Default.GetString(array);
					处理长度 += array.Length;
					byte[] array2 = new byte[int.Parse(@string)];
					Buffer.BlockCopy(var, 处理长度, array2, 0, array2.Length);
					string string2 = Encoding.Default.GetString(array2);
					处理长度 += array2.Length + 1;
					return string2;
				}
			}
			return "";
		}

		private string getStringtou(byte[] var, ref int 处理长度)
		{
			int num = 处理长度;
			for (int i = num; i < var.Length; i++)
			{
				if (var[i] == 32 || var[i] == 10)
				{
					byte[] array = new byte[i - num];
					Buffer.BlockCopy(var, 处理长度, array, 0, array.Length);
					string @string = Encoding.Default.GetString(array);
					处理长度 += array.Length + 1;
					return @string;
				}
			}
			return "";
		}

		private byte[] Write(byte[] 数据)
		{
			int num = 0;
			while ((long)num < 数据.LongLength)
			{
				for (int i = 0; i < this.byte_1A24628.Length; i++)
				{
					if (数据[num] == this.byte_1A24628[i])
					{
						数据[num] = (byte)i;
						break;
					}
				}
				num++;
			}
			return 数据;
		}

		private byte WriteByte(byte 数据)
		{
			for (int i = 0; i < this.byte_1A24628.Length; i++)
			{
				if (数据 == this.byte_1A24628[i])
				{
					数据 = (byte)i;
					break;
				}
			}
			return 数据;
		}

		private void WriteInt(FileStream tdbtt, int 数据)
		{
			byte[] array = this.Write(Encoding.Default.GetBytes(数据.ToString()));
			tdbtt.Write(array, 0, array.Length);
			tdbtt.WriteByte(this.WriteByte(32));
		}

		private void WriteString(FileStream tdbtt, string 数据)
		{
			byte[] array = this.Write(Encoding.Default.GetBytes(数据));
			this.WriteInt(tdbtt, array.Length);
			tdbtt.Write(array, 0, array.Length);
			tdbtt.WriteByte(this.WriteByte(32));
		}

		private void 打开YbqcfgToolStripMenuItem_Click(object sender, EventArgs e)
		{
			try
			{
				OpenFileDialog openFileDialog = new OpenFileDialog();
				openFileDialog.Filter = "Ybq.cfg file|Ybq.cfg";
				openFileDialog.FilterIndex = 1;
				openFileDialog.RestoreDirectory = true;
				if (openFileDialog.ShowDialog() == DialogResult.OK)
				{
					this.任务.Clear();
					this.listView.Items.Clear();
					using (FileStream fileStream = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
					{
						using (BinaryReader binaryReader = new BinaryReader(fileStream))
						{
							MemoryStream memoryStream = new MemoryStream();
							byte[] array = new byte[binaryReader.BaseStream.Length];
							while (binaryReader.Read(array, 0, array.Length) > 0)
							{
								int num = 0;
								this.文件地区 = this.getStringtou(array, ref num);
								this.文件时间 = this.getStringtou(array, ref num);
								this.getlen(array, ref num);
								int num2 = num;
								while ((long)num2 < array.LongLength)
								{
									if ((long)(num2 + 1) >= array.LongLength)
									{
										byte value = this.byte_1A24628[(int)array[num2]];
										memoryStream.WriteByte(value);
									}
									else if (array[num2] == 13 && array[num2 + 1] == 10)
									{
										Console.WriteLine(num2);
									}
									else
									{
										byte value2 = this.byte_1A24628[(int)array[num2]];
										memoryStream.WriteByte(value2);
									}
									num2++;
								}
							}
							byte[] array2 = memoryStream.ToArray();
							int i = 0;
							while (i < array2.Length)
							{
								try
								{
									任务类 任务类 = new 任务类();
									任务类.任务ID = this.getlen(array2, ref i);
									任务类.任务名 = this.getString(array2, ref i);
									任务类.任务等级 = this.getlen(array2, ref i);
									任务类.任务未知1 = this.getlen(array2, ref i);
									任务类.任务未知2 = this.getlen(array2, ref i);
									任务类.任务未知3 = this.getlen(array2, ref i);
									任务类.任务未知4 = this.getlen(array2, ref i);
									任务类.任务未知5 = this.getlen(array2, ref i);
									任务类.任务未知6 = this.getlen(array2, ref i);
									任务类.任务未知7 = this.getlen(array2, ref i);
									任务类.任务接受提示1 = this.getString(array2, ref i);
									任务类.任务拒绝提示1 = this.getString(array2, ref i);
									任务类.任务接受提示2 = this.getString(array2, ref i);
									任务类.任务拒绝提示2 = this.getString(array2, ref i);
									int num3 = this.getlen(array2, ref i);
									for (int j = 0; j < num3; j++)
									{
										任务奖励物品类 任务奖励物品类 = new 任务奖励物品类();
										任务奖励物品类.物品ID = this.getlen(array2, ref i);
										任务奖励物品类.物品数量 = this.getlen(array2, ref i);
										任务类.奖励物品.Add(任务奖励物品类);
									}
									任务类.任务阶段数量 = this.getlen(array2, ref i);
									if (任务类.任务阶段数量 > 0)
									{
										任务类.NpcID = this.getlen(array2, ref i);
										任务类.Npc未知1 = this.getlen(array2, ref i);
										任务类.Npc坐标.地图ID = this.getlen(array2, ref i);
										任务类.Npc坐标.坐标X = this.getlen(array2, ref i);
										任务类.Npc坐标.坐标Y = this.getlen(array2, ref i);
										int num4 = this.getlen(array2, ref i);
										for (int k = 0; k < num4; k++)
										{
											任务需要物品类 任务需要物品类 = new 任务需要物品类();
											任务需要物品类.物品ID = this.getlen(array2, ref i);
											任务需要物品类.物品数量 = this.getlen(array2, ref i);
											任务需要物品类.地图ID = this.getlen(array2, ref i);
											任务需要物品类.坐标X = this.getlen(array2, ref i);
											任务需要物品类.坐标Y = this.getlen(array2, ref i);
											任务类.需要物品.Add(任务需要物品类);
										}
										任务类.任务欢迎接受提示1 = this.getString(array2, ref i);
										任务类.任务欢迎接受提示1 = 任务类.任务欢迎接受提示1.Replace(this.增加内容, "");
										任务类.任务欢迎接受提示2 = this.getString(array2, ref i);
										任务类.任务欢迎接受提示3 = this.getString(array2, ref i);
										任务类.任务欢迎接受提示4 = this.getString(array2, ref i);
										任务类.任务欢迎接受提示5 = this.getString(array2, ref i);
										任务类.任务欢迎拒绝提示1 = this.getString(array2, ref i);
										任务类.任务欢迎拒绝提示1 = 任务类.任务欢迎拒绝提示1.Replace(this.增加内容, "");
										任务类.任务欢迎拒绝提示2 = this.getString(array2, ref i);
										任务类.任务欢迎拒绝提示3 = this.getString(array2, ref i);
										任务类.任务欢迎拒绝提示4 = this.getString(array2, ref i);
										任务类.任务欢迎拒绝提示5 = this.getString(array2, ref i);
										for (int l = 0; l < 任务类.任务阶段数量 - 1; l++)
										{
											任务阶段类 任务阶段类 = new 任务阶段类();
											任务阶段类.任务阶段内容 = this.getString(array2, ref i);
											任务阶段类.NpcID = this.getlen(array2, ref i);
											任务阶段类.Npc未知1 = this.getlen(array2, ref i);
											任务阶段类.Npc地图ID = this.getlen(array2, ref i);
											任务阶段类.Npc坐标X = this.getlen(array2, ref i);
											任务阶段类.Npc坐标Y = this.getlen(array2, ref i);
											int num5 = this.getlen(array2, ref i);
											for (int m = 0; m < num5; m++)
											{
												任务需要物品类 任务需要物品类2 = new 任务需要物品类();
												任务需要物品类2.物品ID = this.getlen(array2, ref i);
												任务需要物品类2.物品数量 = this.getlen(array2, ref i);
												任务需要物品类2.地图ID = this.getlen(array2, ref i);
												任务需要物品类2.坐标X = this.getlen(array2, ref i);
												任务需要物品类2.坐标Y = this.getlen(array2, ref i);
												任务阶段类.需要物品.Add(任务需要物品类2);
											}
											任务阶段类.任务条件符合提示1 = this.getString(array2, ref i);
											任务阶段类.任务条件符合提示2 = this.getString(array2, ref i);
											任务阶段类.任务条件符合提示3 = this.getString(array2, ref i);
											任务阶段类.任务条件符合提示4 = this.getString(array2, ref i);
											任务阶段类.任务条件符合提示5 = this.getString(array2, ref i);
											任务阶段类.任务条件不符合提示1 = this.getString(array2, ref i);
											任务阶段类.任务条件不符合提示2 = this.getString(array2, ref i);
											任务阶段类.任务条件不符合提示3 = this.getString(array2, ref i);
											任务阶段类.任务条件不符合提示4 = this.getString(array2, ref i);
											任务阶段类.任务条件不符合提示5 = this.getString(array2, ref i);
											任务类.任务阶段.Add(任务阶段类);
										}
										int num6 = this.getlen(array2, ref i);
										if (num6 > 0)
										{
											Console.WriteLine(num6);
										}
										i++;
									}
									this.任务.Add(任务类.任务ID, 任务类);
									YbQForm.锁定长度 = i;
								}
								catch
								{
								}
							}
							foreach (任务类 current in this.任务.Values)
							{
								string[] items = new string[]
								{
									current.任务ID.ToString(),
									current.任务名
								};
								this.listView.Items.Insert(this.listView.Items.Count, new ListViewItem(items));
							}
							this.toolStripStatusLabel1.Text = "任务总数:" + this.任务.Count;
						}
					}
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(2, "打开Ybq.cfg出错-" + ex.Message);
			}
		}

		private void 保存YbqcfgToolStripMenuItem_Click(object sender, EventArgs e)
		{
			try
			{
				SaveFileDialog saveFileDialog = new SaveFileDialog();
				saveFileDialog.Filter = "cfg files (*.cfg)|*.cfg|All files (*.*)|*.*";
				saveFileDialog.FilterIndex = 1;
				saveFileDialog.RestoreDirectory = true;
				if (saveFileDialog.ShowDialog() == DialogResult.OK)
				{
					using (FileStream fileStream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write, FileShare.Read))
					{
						byte[] bytes = Encoding.Default.GetBytes(this.文件地区);
						byte[] bytes2 = Encoding.Default.GetBytes(this.文件时间);
						byte[] bytes3 = Encoding.Default.GetBytes(this.任务.Count.ToString());
						fileStream.Write(bytes, 0, bytes.Length);
						fileStream.WriteByte(32);
						fileStream.Write(bytes2, 0, bytes2.Length);
						fileStream.WriteByte(10);
						fileStream.Write(bytes3, 0, bytes3.Length);
						fileStream.WriteByte(32);
						foreach (任务类 current in this.任务.Values)
						{
							this.WriteInt(fileStream, current.任务ID);
							this.WriteString(fileStream, current.任务名);
							this.WriteInt(fileStream, current.任务等级);
							this.WriteInt(fileStream, current.任务未知1);
							this.WriteInt(fileStream, current.任务未知2);
							this.WriteInt(fileStream, current.任务未知3);
							this.WriteInt(fileStream, current.任务未知4);
							this.WriteInt(fileStream, current.任务未知5);
							this.WriteInt(fileStream, current.任务未知6);
							this.WriteInt(fileStream, current.任务未知7);
							this.WriteString(fileStream, current.任务接受提示1);
							this.WriteString(fileStream, current.任务拒绝提示1);
							this.WriteString(fileStream, current.任务接受提示2);
							this.WriteString(fileStream, current.任务拒绝提示2);
							this.WriteInt(fileStream, current.奖励物品.Count);
							if (current.奖励物品.Count > 0)
							{
								foreach (任务奖励物品类 current2 in current.奖励物品)
								{
									this.WriteInt(fileStream, current2.物品ID);
									this.WriteInt(fileStream, current2.物品数量);
								}
							}
							if (current.任务阶段数量 > 0)
							{
								this.WriteInt(fileStream, current.任务阶段.Count + 1);
								this.WriteInt(fileStream, current.NpcID);
								this.WriteInt(fileStream, current.Npc未知1);
								this.WriteInt(fileStream, current.Npc坐标.地图ID);
								this.WriteInt(fileStream, current.Npc坐标.坐标X);
								this.WriteInt(fileStream, current.Npc坐标.坐标Y);
								this.WriteInt(fileStream, current.需要物品.Count);
								if (current.需要物品.Count > 0)
								{
									foreach (任务需要物品类 current3 in current.需要物品)
									{
										this.WriteInt(fileStream, current3.物品ID);
										this.WriteInt(fileStream, current3.物品数量);
										this.WriteInt(fileStream, current3.地图ID);
										this.WriteInt(fileStream, current3.坐标X);
										this.WriteInt(fileStream, current3.坐标Y);
									}
								}
								try
								{
									if (current.任务欢迎接受提示1.Length > 10)
									{
										while (current.任务欢迎接受提示1.LastIndexOf("\r\n") >= current.任务欢迎接受提示1.Length - 2)
										{
											current.任务欢迎接受提示1 = current.任务欢迎接受提示1.Remove(current.任务欢迎接受提示1.Length - 2, 2);
										}
									}
									if (current.任务欢迎拒绝提示1.Length > 10)
									{
										while (current.任务欢迎拒绝提示1.LastIndexOf("\r\n") >= current.任务欢迎拒绝提示1.Length - 2)
										{
											current.任务欢迎拒绝提示1 = current.任务欢迎拒绝提示1.Remove(current.任务欢迎拒绝提示1.Length - 2, 2);
										}
									}
								}
								catch (Exception)
								{
								}
								this.WriteString(fileStream, current.任务欢迎接受提示1 + this.增加内容);
								this.WriteString(fileStream, current.任务欢迎接受提示2);
								this.WriteString(fileStream, current.任务欢迎接受提示3);
								this.WriteString(fileStream, current.任务欢迎接受提示4);
								this.WriteString(fileStream, current.任务欢迎接受提示5);
								this.WriteString(fileStream, current.任务欢迎拒绝提示1 + this.增加内容);
								this.WriteString(fileStream, current.任务欢迎拒绝提示2);
								this.WriteString(fileStream, current.任务欢迎拒绝提示3);
								this.WriteString(fileStream, current.任务欢迎拒绝提示4);
								this.WriteString(fileStream, current.任务欢迎拒绝提示5);
								foreach (任务阶段类 current4 in current.任务阶段)
								{
									this.WriteString(fileStream, current4.任务阶段内容);
									this.WriteInt(fileStream, current4.NpcID);
									this.WriteInt(fileStream, current4.Npc未知1);
									this.WriteInt(fileStream, current4.Npc地图ID);
									this.WriteInt(fileStream, current4.Npc坐标X);
									this.WriteInt(fileStream, current4.Npc坐标Y);
									this.WriteInt(fileStream, current4.需要物品.Count);
									if (current4.需要物品.Count > 0)
									{
										foreach (任务需要物品类 current5 in current4.需要物品)
										{
											this.WriteInt(fileStream, current5.物品ID);
											this.WriteInt(fileStream, current5.物品数量);
											this.WriteInt(fileStream, current5.地图ID);
											this.WriteInt(fileStream, current5.坐标X);
											this.WriteInt(fileStream, current5.坐标Y);
										}
									}
									this.WriteString(fileStream, current4.任务条件符合提示1);
									this.WriteString(fileStream, current4.任务条件符合提示2);
									this.WriteString(fileStream, current4.任务条件符合提示3);
									this.WriteString(fileStream, current4.任务条件符合提示4);
									this.WriteString(fileStream, current4.任务条件符合提示5);
									this.WriteString(fileStream, current4.任务条件不符合提示1);
									this.WriteString(fileStream, current4.任务条件不符合提示2);
									this.WriteString(fileStream, current4.任务条件不符合提示3);
									this.WriteString(fileStream, current4.任务条件不符合提示4);
									this.WriteString(fileStream, current4.任务条件不符合提示5);
								}
								this.WriteInt(fileStream, 0);
								fileStream.WriteByte(this.WriteByte(10));
							}
							else
							{
								byte[] array = this.Write(Encoding.Default.GetBytes(current.任务阶段.Count.ToString()));
								fileStream.Write(array, 0, array.Length);
								fileStream.WriteByte(this.WriteByte(10));
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(2, "保存Ybq.cfg出错-" + ex.Message);
			}
		}

		private void listView_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.listView.SelectedItems.Count > 0)
			{
				string text = this.listView.SelectedItems[0].SubItems[0].Text;
				任务类 对象;
				if (this.任务.TryGetValue(int.Parse(text), out 对象))
				{
					CustomPropertyCollection customPropertyCollection = new CustomPropertyCollection();
					customPropertyCollection.Add(new CustomProperty("任务ID", "任务ID", true, "基本", "任务ID", 对象));
					customPropertyCollection.Add(new CustomProperty("任务名", "任务名", false, "基本", "任务名", 对象));
					customPropertyCollection.Add(new CustomProperty("任务等级", "任务等级", false, "基本", "任务等级", 对象));
					customPropertyCollection.Add(new CustomProperty("任务拒绝提示1", "任务拒绝提示1", false, "基本", "拒绝接受任务后的提示", 对象, typeof(MultilineStringEditor)));
					customPropertyCollection.Add(new CustomProperty("任务拒绝提示2", "任务拒绝提示2", false, "基本", "拒绝接受任务后的提示", 对象, typeof(MultilineStringEditor)));
					customPropertyCollection.Add(new CustomProperty("任务接受提示1", "任务接受提示1", false, "基本", "接受任务后的提示", 对象, typeof(MultilineStringEditor)));
					customPropertyCollection.Add(new CustomProperty("任务接受提示2", "任务接受提示2", false, "基本", "接受任务后的提示", 对象, typeof(MultilineStringEditor)));
					customPropertyCollection.Add(new CustomProperty("NpcID", "NpcID", false, "Npc", "NpcID", 对象));
					customPropertyCollection.Add(new CustomProperty("Npc坐标", "Npc坐标", false, "Npc", "Npc坐标", 对象, true));
					customPropertyCollection.Add(new CustomProperty("奖励物品列表", "奖励物品", false, "奖励物品", "奖励物品", 对象, typeof(My奖励物品CollectionEditor)));
					customPropertyCollection.Add(new CustomProperty("需要物品列表", "需要物品", false, "需要物品", "需要物品", 对象, typeof(My需要物品CollectionEditor)));
					customPropertyCollection.Add(new CustomProperty("不符合内容", "任务欢迎拒绝提示1", false, "任务内容", "打开任务后条件不符合的提示内容", 对象, typeof(MultilineStringEditor)));
					customPropertyCollection.Add(new CustomProperty("符合内容", "任务欢迎接受提示1", false, "任务内容", "打开任务后条件符合的提示内容", 对象, typeof(MultilineStringEditor)));
					customPropertyCollection.Add(new CustomProperty("阶段数量", "任务阶段数量", false, "任务阶段", "任务阶段总数量=阶段列表的数量+1", 对象));
					customPropertyCollection.Add(new CustomProperty("阶段列表", "任务阶段", false, "任务阶段", "任务阶段列表", 对象, typeof(My任务阶段CollectionEditor)));
					this.propertyGrid1.SelectedObject = customPropertyCollection;
				}
			}
		}
	}
}
